/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.diagram;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

import jdbcmanager.swing.*;
import jdbcmanager.diagram.swing.*;
import jdbcmanager.dbtools.datatypes.*;

/**
 * This class uses information stored in an instance of 
 * <code>RelationDefinition</code> to create a JConnector.
 *
 * @author Francisco Morero Peyrona
 */

final class JRelation extends JConnector
{
   private RelationDefinition reldef;

   //-------------------------------------------------------------------------//

   JRelation( RelationDefinition rel )
   {
      super(); // The zero-argument constructor is used

      this.reldef = rel;
      
      b1toN = this.reldef.is1toN();   // b1toN defined in super-class
   }

   String getPrimaryTable()
   {
      return this.reldef.getPrimaryTable();
   }

   String getForeignTable()
   {
      return this.reldef.getForeignTable();
   }

   String getPrimaryField()
   {
      return this.reldef.getPrimaryColumn();
   }

   String getForeignField()
   {
      return this.reldef.getForeignColumn();
   }

   boolean isCircular()
   {
      return reldef.getPrimaryTable().equals( reldef.getForeignTable() );
   }

   public boolean equals( JRelation relation )
   {
      return this.reldef.equals( relation.reldef );
   }

   public String toString()
   {
      return "Point1 = "+ this.getLine().getP1() +
             "Point2 = "+ this.getLine().getP1() +
             this.reldef.toString();
   }

   /**
    * Invoked when Double-Clicked
    */
   protected void showProperties()
   {
      JxDialog dlg = new JxDialog( "Relation Properties" );
               dlg.getContentPane().add( new PropertiesPanel( this.reldef ) );
               dlg.setVisibleButtonPanel( false );
               dlg.pack();
               dlg.center();
               dlg.setVisible( true );
   }
   
   //-------------------------------------------------------------------------//
   // INNER CLASS: PropertiesPanel
   //-------------------------------------------------------------------------//
   final class PropertiesPanel extends JPanel
   {
      private JPanel     pnlPrimary      = new JPanel();
      private JLabel     lblPrimaryTable = new JLabel();
      private JLabel     lblPrimaryField = new JLabel();
      private JLabel     lblPrimaryKey   = new JLabel();
      private JTextField txtPrimaryTable = new JTextField();
      private JTextField txtPrimaryField = new JTextField();
      private JTextField txtPrimaryKey   = new JTextField();
      private JTextField txtForeignField = new JTextField();

      private JPanel     pnlForeign      = new JPanel();
      private JLabel     lblForeignField = new JLabel();
      private JLabel     lblForeignKey   = new JLabel();
      private JTextField txtForeignKey   = new JTextField();
      private JTextField txtForeignTable = new JTextField();
      private JLabel     lblForeignTable = new JLabel();

      PropertiesPanel( RelationDefinition reldef )
      {
         init();

         txtPrimaryTable.setText( reldef.getPrimaryTable() );
         txtPrimaryField.setText( reldef.getPrimaryColumn() );
         txtPrimaryKey.setText(   reldef.getPrimaryKey()   );

         txtForeignTable.setText( reldef.getForeignTable() );
         txtForeignField.setText( reldef.getForeignColumn() );
         txtForeignKey.setText(   reldef.getForeignKey()   );
      }

      private void init()
      {
         // Panel Primary -------------------------
         pnlPrimary.setBorder( new TitledBorder( BorderFactory.createLineBorder( SystemColor.controlText, 1 ), " Primary " ) );
         pnlPrimary.setLayout( new GridBagLayout() );

         lblPrimaryTable.setText( "Table" );
         lblPrimaryField.setText( "Field" );
         lblPrimaryKey.setText( "Key" );
         txtPrimaryTable.setEditable( false );
         txtPrimaryField.setEditable( false );
         txtPrimaryKey.setEditable( false );

         pnlPrimary.add( lblPrimaryTable,
                         new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 4, 0, 0 ), 9, 9 ) );
         pnlPrimary.add( lblPrimaryField,
                         new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 8, 4, 0, 0 ), 13, 9 ) );
         pnlPrimary.add( txtPrimaryField,
                         new GridBagConstraints( 1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 8, 0, 0, 0 ), 287, 3 ) );
         pnlPrimary.add( lblPrimaryKey,
                         new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 8, 4, 0, 0 ), 16, 9 ) );
         pnlPrimary.add( txtPrimaryKey,
                         new GridBagConstraints( 1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 8, 0, 0, 0 ), 287, 3 ) );
         pnlPrimary.add( txtPrimaryTable,
                         new GridBagConstraints( 1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 287, 3 ) );


         // Panel Foreign --------------------------
         pnlForeign.setBorder( new TitledBorder( BorderFactory.createLineBorder( SystemColor.controlText, 1 ), " Foreing " ) );
         pnlForeign.setLayout( new GridBagLayout() );

         lblForeignTable.setText( "Table" );
         lblForeignField.setText( "Field" );
         lblForeignKey.setText( "Key" );
         txtForeignTable.setEditable( false );
         txtForeignField.setEditable( false );
         txtForeignKey.setEditable( false );

         pnlForeign.add( lblForeignKey,
                         new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 10, 3, 0, 0 ), 14, 9 ) );
         pnlForeign.add( lblForeignField,
                         new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 10, 3, 0, 0 ), 17, 9 ) );
         pnlForeign.add( lblForeignTable,
                         new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 3, 0, 0 ), 10, 9 ) );
         pnlForeign.add( txtForeignKey,
                         new GridBagConstraints( 1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 10, 0, 0, 0 ), 287, 3 ) );
         pnlForeign.add( txtForeignField,
                         new GridBagConstraints( 1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 10, 0, 0, 0 ), 287, 3 ) );
         pnlForeign.add( txtForeignTable,
                         new GridBagConstraints( 1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 287, 3 ) );


         // Adding to panel -------------------------
         setLayout( new jdbcmanager.awt.VerticalFlowLayout( SwingConstants.LEADING, 10, 10, SwingConstants.LEADING ) );
         add( pnlPrimary );
         add( pnlForeign );
      }
   }
}